package com.gonsin.gmessage.logic.websocket;

import com.gonsin.gmessage.logic.model.GMessagePageRequest;
import com.gonsin.gmessage.logic.model.GMessagePageResult;
import com.gonsin.gmessage.logic.model.GMessageResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "系统相关接口")
@RequestMapping("/api/system/websocket")
public class WebSocketController  {

    @Autowired private WebSocketSessionContainer sessionContainer;

    @GetMapping("/online")
    @ApiOperation("WEBSOCKET - 查询在线的WebSocket终端")
    public GMessageResponseResult<GMessagePageResult<SessionInfo>> getOnlineClient(GMessagePageRequest pagePo){
        GMessagePageResult<SessionInfo> sessions = sessionContainer.getOnline(pagePo);
        return GMessageResponseResult.result(sessions);
    }



//    @PostMapping("/test/broadcast")
//    @ApiOperation("测试广播websocket指令")
//    public ResponseResult<Void> broadcastCmd(
//            @ApiParam("cmd指令") @RequestParam("cmd") String cmd,
//            @ApiParam("subCmd指令") @RequestParam("cmd") String subCmd,
//            @ApiParam("args参数（json）") @RequestParam(value = "args", required = false, defaultValue = "{}") String argsJson){
//        Map<String, Object> args = JSONUtils.jsonToMap(argsJson);
//        MessageBo message = MessageBo.create().cmd(cmd).subCmd(subCmd).args(args);
//        sessionContainer.broadcastCmd(cmd, message);
//        return ResponseResult.simpleResult();
//    }

//    @PostMapping("/test/broadcast")
//    @ApiOperation("测试广播websocket指令")
//    public ResponseResult<Void> broadcastCmd(
//            @RequestBody MessageBo message){
////        Map<String, Object> args = JSONUtils.jsonToMap(argsJson);
////        MessageBo message = MessageBo.create().cmd(cmd).subCmd(subCmd).args(args);
//        sessionContainer.broadcastCmd(message.getCmd().getCmd(), message);
//        return ResponseResult.simpleResult();
//    }

//    @PostMapping("/test/send")
//    @ApiOperation("测试发送单个websocket指令")
//    public ResponseResult<Void> sendCmd(
//            @ApiParam("连接的用户名") @RequestParam("username") String username,
//            @ApiParam("cmd指令") @RequestParam("cmd") String cmd,
//            @ApiParam("subCmd指令") @RequestParam("cmd") String subCmd,
//            @ApiParam("args参数（json）") @RequestParam(value = "args", required = false, defaultValue = "{}") String argsJson){
//        Map<String, Object> args = JSONUtils.jsonToMap(argsJson);
//        MessageBo message = MessageBo.create().cmd(cmd).subCmd(subCmd).args(args);
//        sessionContainer.sendCmd(username, message);
//        return ResponseResult.simpleResult();
//    }

//    @PostMapping("/test/send/{username}")
//    @ApiOperation("测试发送单个websocket指令")
//    public ResponseResult<Void> sendCmd(
//            @ApiParam("用户名") @PathVariable("username") String username,
//            @RequestBody MessageBo messageBo){
////        Map<String, Object> args = JSONUtils.jsonToMap(argsJson);
////        MessageBo message = MessageBo.create().cmd(cmd).subCmd(subCmd).args(args);
//        sessionContainer.sendCmd(username, messageBo);
//        return ResponseResult.simpleResult();
//    }

}
